package com.example.gzysmartcity.ui.fragment.sell;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentSellcarBinding;
import com.example.gzysmartcity.databinding.PopuSell2Binding;
import com.example.gzysmartcity.ui.adapter.SellCarRvAdpater;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.utils.PopupUntil;
import com.example.gzysmartcity.viewmodel.SellCarViewModel;

import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;

public class SellCarFragment extends BaseFragment<FragmentSellcarBinding> {
    @Override
    protected FragmentSellcarBinding viewBinding() {
        return FragmentSellcarBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    private double price;

    @SuppressLint({"SetTextI18n", "NonConstantResourceId"})
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SellCarViewModel sellCarViewModel = new ViewModelProvider(this).get(SellCarViewModel.class);
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        SellCarRvAdpater adapter = new SellCarRvAdpater(R.layout.rv_sellcar, sellCarViewModel.getSellCarListSize());
        adapter.setNewInstance(sellCarViewModel.getSellCarList());
        b.rvSellCar.setAdapter(adapter);
        b.rvSellCar.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.addChildClickViewIds(R.id.rv_sellCar_add, R.id.rv_sellCar_remove, R.id.rv_btn_remove, R.id.rv_sellCar_box);
        adapter.setOnItemChildClickListener((adapter1, view1, position) -> {
//            SellCar car = sellCarList.get(position);
//            long num = sellCarList.get(position).getNum();
            if (view1.getId() == R.id.rv_sellCar_box) {
                if (((CheckBox) view1).isChecked()) {
                    //true
                    //增加
                    price += sellCarViewModel.getPrice(position);
                    adapter.setBoolList(position, true);
                } else {
                    //false
                    //减少
                    price -= sellCarViewModel.getPrice(position);
                    adapter.setBoolList(position, false);
                }
            } else {
                CheckBox checkBox = (CheckBox) adapter1.getViewByPosition(position, R.id.rv_sellCar_box);
                if (checkBox == null) return;
                boolean checked = checkBox.isChecked();
                switch (view1.getId()) {
                    case R.id.rv_sellCar_add:
                        if (checked) {
                            price += sellCarViewModel.AddSellCarNum(position);
                        } else {
                            sellCarViewModel.AddSellCarNum(position);
                        }
                        adapter.notifyItemChanged(position);

                        break;
                    case R.id.rv_sellCar_remove:
                        double v = sellCarViewModel.ReduceSellCarNum(position);
                        if (v == 0) {
                            ToastUtils.showShort("最少选择一件");
                            return;
                        }
                        if (checked) {
                            price -= v;
                        }
                        adapter.notifyItemChanged(position);
                        break;
                    case R.id.rv_btn_remove:
                        if (checked) {
                            price -= sellCarViewModel.DeleteSellCar(position);
                        } else {
                            sellCarViewModel.DeleteSellCar(position);
                        }
                        adapter.notifyDataSetChangedWithRemove(position);
                        break;
                }
            }
            b.tvSellCarAll.setText("合计：" + decimalFormat.format(price) + "元");
        });
        b.sellCarChackAll.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                adapter.setCheck(true);
                price = sellCarViewModel.getSumNumAll();
                b.tvSellCarAll.setText("合计：" + decimalFormat.format(price) + "元");
            } else {
                adapter.setCheck(false);
                price = 0;
                b.tvSellCarAll.setText("合计：0元");
            }
            adapter.notifyDataSetChanged();

        });
        b.btnSellCarJiesuan.setOnClickListener(v -> {
            if (price == 0) {
                return;
            }
            PopupUntil until = new PopupUntil();
            PopuSell2Binding p2 = PopuSell2Binding.inflate(getLayoutInflater());
            until.getPopupDataByView(p2.getRoot(), requireActivity());
//            List<Boolean> list = adapter.getList();
            sellCarViewModel.DeleteSomeSellCar(adapter.getList());
//            for (int i = 0; i < list.size(); i++) {
//                Log.i("TAG", "remove:" + list.get(i));
//                if (list.get(i)) {
//                    price -= sellCarViewModel.DeleteSomeSellCar(i);
//                    list.set(i, false);
//                }
//            }
            b.tvSellCarAll.setText("合计：0元");
            adapter.notifyDataSetChanged();
            p2.p2TvTishi.setText("购买成功");
            //Toast提示【购买成功】并等待2秒后返回
            new Handler().postDelayed(() -> {
                //购买成功，清理购买条例，返回购买首页
                until.closePopup();
                remove();
            }, 2000);
        });
    }

    @Override
    protected String setTitle() {
        return "购物车";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return false;
    }
}
